﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Collections;

namespace Cat.Domain.Caching
{
    public partial class PerRequestCacheManager : ICacheManager
    {
        protected IDictionary GetItems()
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Items;
            }
            return null;
        }


        public T Get<T>(string key)
        {
            var items = GetItems();
            if (items == null)
                return default(T);

            return (T)items[key];
        }

        public object Get(string key)
        {
            var items = GetItems();
            if (items == null)
                return null;

            return items[key];
        }

        public void Set(string key, object data)
        {
            Set(key, data, 0);
        }

        public void Set(string key, object data, int cacheTime)
        {
            var items = GetItems();
            if (items == null)
            {
                return;
            }
            if (data != null)
            {
                if (items.Contains(key))
                {
                    items[key] = data;
                }
                else
                {
                    items.Add(key, data);
                }
            }
        }


        public bool IsSet(string key)
        {
            var items = GetItems();
            if (items == null)
            {
                return false;
            }
            return (items[key] != null);
        }


        public void Remove(string key)
        {
            var items = GetItems();
            if (items == null)
            {
                return;
            }
            var value = Get(key);
            if (value != null)
            {
                if (value is IDisposable)
                {
                    (value as IDisposable).Dispose();
                }
            }
            items.Remove(key);
        }


        public void RemoveByPattern(string pattern)
        {
            var items = GetItems();
            if (items == null)
                return;

            var enumerator = items.GetEnumerator();
            var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            var keysToRemove = new List<String>();
            while (enumerator.MoveNext())
            {
                if (regex.IsMatch(enumerator.Key.ToString()))
                {
                    keysToRemove.Add(enumerator.Key.ToString());
                }
            }

            foreach (string key in keysToRemove)
            {
                items.Remove(key);
            }
        }


        public void Clear()
        {
            var items = GetItems();
            if (items == null)
                return;

            var enumerator = items.GetEnumerator();
            var keysToRemove = new List<String>();
            while (enumerator.MoveNext())
            {
                keysToRemove.Add(enumerator.Key.ToString());
            }

            foreach (string key in keysToRemove)
            {
                items.Remove(key);
            }
        }
    }
}
